package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.MyRingBean;
import flc.ast.bean.MyTimerBean;
import flc.ast.databinding.ActivityTimerEditBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;

public class TimerEditActivity extends BaseAc<ActivityTimerEditBinding> {

    public static MyTimerBean myBean = null;
    private Integer music = R.raw.music1;

    @Override
    protected int onCreate() {
        return R.layout.activity_timer_edit;
    }

    @Override
    protected void initView() {
        mDataBinding.ivTimerEditBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvRing.setOnClickListener(this);
        mDataBinding.tvSave.setOnClickListener(this);
        mDataBinding.tvPreset1.setOnClickListener(this);
        mDataBinding.tvPreset2.setOnClickListener(this);
        mDataBinding.tvPreset3.setOnClickListener(this);
        mDataBinding.tvPreset4.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvRing://铃声
                RingActivity.selName = mDataBinding.tvRing.getText().toString();
                Intent intent = new Intent(this, RingActivity.class);
                startActivityForResult(intent, Content.SEL_RING);
                break;
            case R.id.tvPreset1://1分钟
                mDataBinding.wheelView1.setCurrentItem(0);
                mDataBinding.wheelView2.setCurrentItem(1);
                mDataBinding.wheelView3.setCurrentItem(0);
                mDataBinding.tvTime.setText(mDataBinding.wheelView1.getCurrentItem() + getString(R.string.hour_text) + mDataBinding.wheelView2.getCurrentItem() + getString(R.string.minute_text) + mDataBinding.wheelView3.getCurrentItem() + getString(R.string.second_text));
                break;
            case R.id.tvPreset2://2分钟
                mDataBinding.wheelView1.setCurrentItem(0);
                mDataBinding.wheelView2.setCurrentItem(2);
                mDataBinding.wheelView3.setCurrentItem(0);
                mDataBinding.tvTime.setText(mDataBinding.wheelView1.getCurrentItem() + getString(R.string.hour_text) + mDataBinding.wheelView2.getCurrentItem() + getString(R.string.minute_text) + mDataBinding.wheelView3.getCurrentItem() + getString(R.string.second_text));
                break;
            case R.id.tvPreset3://3分钟
                mDataBinding.wheelView1.setCurrentItem(0);
                mDataBinding.wheelView2.setCurrentItem(3);
                mDataBinding.wheelView3.setCurrentItem(0);
                mDataBinding.tvTime.setText(mDataBinding.wheelView1.getCurrentItem() + getString(R.string.hour_text) + mDataBinding.wheelView2.getCurrentItem() + getString(R.string.minute_text) + mDataBinding.wheelView3.getCurrentItem() + getString(R.string.second_text));
                break;
            case R.id.tvPreset4://4分钟
                mDataBinding.wheelView1.setCurrentItem(0);
                mDataBinding.wheelView2.setCurrentItem(4);
                mDataBinding.wheelView3.setCurrentItem(0);
                mDataBinding.tvTime.setText(mDataBinding.wheelView1.getCurrentItem() + getString(R.string.hour_text) + mDataBinding.wheelView2.getCurrentItem() + getString(R.string.minute_text) + mDataBinding.wheelView3.getCurrentItem() + getString(R.string.second_text));
                break;
            case R.id.tvSave://保存
                if (myBean == null) {
                    saveTimer();
                } else {
                    updateTimer();
                }
                break;
        }
    }

    @Override
    protected void initData() {
        setDateView();
        if (myBean != null) {
            mDataBinding.etTag.setText(myBean.getTag());
            mDataBinding.tvRing.setText(myBean.getRingName());
            mDataBinding.wheelView1.setCurrentItem(myBean.getHour());
            mDataBinding.wheelView2.setCurrentItem(myBean.getMinute());
            mDataBinding.wheelView3.setCurrentItem(myBean.getSecond());
            music = myBean.getRing();
            mDataBinding.tvTime.setText(myBean.getHour() + getString(R.string.hour_text) + myBean.getMinute() + getString(R.string.minute_text) + myBean.getSecond() + getString(R.string.second_text));
        } else {
            mDataBinding.tvRing.setText(getString(R.string.ring_text) + 1);
            mDataBinding.tvTime.setText(mDataBinding.wheelView1.getCurrentItem() + getString(R.string.hour_text) + mDataBinding.wheelView2.getCurrentItem() + getString(R.string.minute_text) + mDataBinding.wheelView3.getCurrentItem() + getString(R.string.second_text));
        }
    }

    //保存计时器
    private void saveTimer() {
        //标签
        String tag = mDataBinding.etTag.getText().toString();
        if (TextUtils.isEmpty(tag)) {
            ToastUtils.showShort(R.string.please_input_tag);
            return;
        }
        //铃声
        String ringName = mDataBinding.tvRing.getText().toString();
        showDialog(getString(R.string.saving));
        //选择的时间
        int hour = mDataBinding.wheelView1.getCurrentItem();
        int minute = mDataBinding.wheelView2.getCurrentItem();
        int second = mDataBinding.wheelView3.getCurrentItem();
        //准备数据
        List<MyTimerBean> listSave = new ArrayList<>();
        listSave.add(new MyTimerBean(TimeUtils.getNowString(), tag, ringName, music, hour, minute, second));
        List<MyTimerBean> listSp = AppSpUtils.getTimerList();
        if (listSp != null && listSp.size() != 0) {
            listSp.addAll(listSave);
            AppSpUtils.savTimerList(listSp);
        } else {
            AppSpUtils.savTimerList(listSave);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissDialog();
                finish();
            }
        }, 500);
    }

    //修改计时器
    private void updateTimer() {
        //标签
        String tag = mDataBinding.etTag.getText().toString();
        if (TextUtils.isEmpty(tag)) {
            ToastUtils.showShort(R.string.please_input_tag);
            return;
        }
        //铃声
        String ringName = mDataBinding.tvRing.getText().toString();
        showDialog(getString(R.string.saving));
        //选择的日期
        int hour = mDataBinding.wheelView1.getCurrentItem();
        int minute = mDataBinding.wheelView2.getCurrentItem();
        int second = mDataBinding.wheelView3.getCurrentItem();
        //保存数据
        List<MyTimerBean> listSp = AppSpUtils.getTimerList();
        if (listSp != null && listSp.size() > 0) {
            for (int i = 0; i < listSp.size(); i++) {
                if (listSp.get(i).getId().equals(myBean.getId())) {
                    listSp.get(i).setTag(tag);
                    listSp.get(i).setRingName(ringName);
                    listSp.get(i).setRing(music);
                    listSp.get(i).setHour(hour);
                    listSp.get(i).setMinute(minute);
                    listSp.get(i).setSecond(second);
                    break;
                }
            }
            AppSpUtils.savTimerList(listSp);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissDialog();
                finish();
            }
        }, 500);
    }

    //设置日前选择布局
    private void setDateView() {
        mDataBinding.wheelView1.setDividerColor(Color.parseColor("#FFFFFF"));
        mDataBinding.wheelView1.setTextColorCenter(Color.parseColor("#33A5FF"));
        mDataBinding.wheelView1.setLineSpacingMultiplier(2.5f);
        mDataBinding.wheelView1.setDividerType(WheelView.DividerType.WRAP);
        mDataBinding.wheelView1.setCurrentItem(0);
        mDataBinding.wheelView1.setItemsVisibleCount(5);
        mDataBinding.wheelView1.setCyclic(true);
        mDataBinding.wheelView1.setTextSize(16);
        mDataBinding.wheelView2.setDividerColor(Color.parseColor("#FFFFFF"));
        mDataBinding.wheelView2.setTextColorCenter(Color.parseColor("#33A5FF"));
        mDataBinding.wheelView2.setLineSpacingMultiplier(2.5f);
        mDataBinding.wheelView2.setDividerType(WheelView.DividerType.WRAP);
        mDataBinding.wheelView2.setCurrentItem(0);
        mDataBinding.wheelView2.setItemsVisibleCount(5);
        mDataBinding.wheelView2.setCyclic(true);
        mDataBinding.wheelView2.setTextSize(16);
        mDataBinding.wheelView3.setDividerColor(Color.parseColor("#FFFFFF"));
        mDataBinding.wheelView3.setTextColorCenter(Color.parseColor("#33A5FF"));
        mDataBinding.wheelView3.setLineSpacingMultiplier(2.5f);
        mDataBinding.wheelView3.setDividerType(WheelView.DividerType.WRAP);
        mDataBinding.wheelView3.setCurrentItem(0);
        mDataBinding.wheelView3.setItemsVisibleCount(5);
        mDataBinding.wheelView3.setCyclic(true);
        mDataBinding.wheelView3.setTextSize(16);
        //设置滚轮数据
        List<String> listHour = new ArrayList<>();//时
        for (int i = 0; i < 24; i++) {
            listHour.add(String.valueOf(i));
        }
        List<String> listMinute = new ArrayList<>();//分
        for (int i = 0; i < 60; i++) {
            listMinute.add(String.valueOf(i));
        }
        List<String> listSecond = new ArrayList<>();//秒
        for (int i = 0; i < 60; i++) {
            listSecond.add(String.valueOf(i));
        }
        WheelAdapter adapter1 = new ArrayWheelAdapter(listHour);
        mDataBinding.wheelView1.setAdapter(adapter1);
        WheelAdapter adapter2 = new ArrayWheelAdapter(listMinute);
        mDataBinding.wheelView2.setAdapter(adapter2);
        WheelAdapter adapter3 = new ArrayWheelAdapter(listMinute);
        mDataBinding.wheelView3.setAdapter(adapter3);

        mDataBinding.wheelView1.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                mDataBinding.tvTime.setText(mDataBinding.wheelView1.getCurrentItem() + getString(R.string.hour_text) + mDataBinding.wheelView2.getCurrentItem() + getString(R.string.minute_text) + mDataBinding.wheelView3.getCurrentItem() + getString(R.string.second_text));
            }
        });

        mDataBinding.wheelView2.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                mDataBinding.tvTime.setText(mDataBinding.wheelView1.getCurrentItem() + getString(R.string.hour_text) + mDataBinding.wheelView2.getCurrentItem() + getString(R.string.minute_text) + mDataBinding.wheelView3.getCurrentItem() + getString(R.string.second_text));
            }
        });

        mDataBinding.wheelView3.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                mDataBinding.tvTime.setText(mDataBinding.wheelView1.getCurrentItem() + getString(R.string.hour_text) + mDataBinding.wheelView2.getCurrentItem() + getString(R.string.minute_text) + mDataBinding.wheelView3.getCurrentItem() + getString(R.string.second_text));
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Content.SEL_RING && resultCode == RESULT_OK) {
            MyRingBean ring = data.getParcelableExtra(Content.RING_DATA);
            mDataBinding.tvRing.setText(ring.getName());
            music = ring.getMusic();
        }
    }

}